﻿/*
Copyright (c) 2013, Marc Tanti
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the www.marctanti.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MARIA.program.statementfactory;
using MARIA.level.square;
using MARIA.level;
using MARIA.level.direction;
using MARIA.program.condition;

namespace MARIA
{
    /// <summary>
    /// A cache of all the variety of objects with limited variety. This allows these objects to not be viewed as their super classes and hence improve abstraction.
    /// </summary>
    public class Cache
    {

        /// <summary>
        /// Get singleton instance.
        /// </summary>
        public static Cache Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Cache();
                }
                return instance;
            }
        }
        private static Cache instance = null;

        /// <summary>
        /// The list of directions.
        /// </summary>
        public Direction[] Directions
        {
            get
            {
                return directions;
            }
        }
        private Direction[] directions;

        /// <summary>
        /// The list of squares.
        /// </summary>
        public Square[] Squares
        {
            get
            {
                return squares;
            }
        }
        private Square[] squares;

        /// <summary>
        /// The list of tests.
        /// </summary>
        public Test[] Tests
        {
            get
            {
                return tests;
            }
        }
        private Test[] tests;

        /// <summary>
        /// The list of statements.
        /// </summary>
        public StatementFactory[] Statements
        {
            get
            {
                return statements;
            }
        }
        private StatementFactory[] statements;

        /// <summary>
        /// The list of levels.
        /// </summary>
        public Level[] Levels
        {
            get
            {
                if (levels == null)
                {
                    LoadDefaultLevels();
                }
                return levels;
            }
        }
        private Level[] levels;

        /// <summary>
        /// Initialize a new instance of Cache.
        /// </summary>
        private Cache()
        {
            directions = new Direction[]
            {
                UpDirection.Instance,
                DownDirection.Instance,
                RightDirection.Instance,
                LeftDirection.Instance,
                HereDirection.Instance,
            };

            squares = new Square[]
            {
                ClearSquare.Instance,
                HoleSquare.Instance,
                GoalSquare.Instance,
                CrossSquare.Instance,
            };

            tests = new Test[]
            {
                EqualityTest.Instance,
                NonequalityTest.Instance,
            };

            statements = new StatementFactory[]
            {
                MoveDownFactory.Instance,
                MoveUpFactory.Instance,
                MoveLeftFactory.Instance,
                MoveRightFactory.Instance,
                IfFactory.Instance,
                ChooseFactory.Instance,
                ChoiceFactory.Instance,
                WhateverFactory.Instance,
                WhileFactory.Instance,
                CallFactory.Instance,
                SubprogramFactory.Instance,
                ProgramFactory.Instance,
                ClipboardFactory.Instance,
            };
        }

        public void LoadDefaultLevels()
        {
            levels = new Level[]
            {
                new Level(Properties.Resources.level_01, squares, statements),
                new Level(Properties.Resources.level_02, squares, statements),
                new Level(Properties.Resources.level_03, squares, statements),
                new Level(Properties.Resources.level_04, squares, statements),
                new Level(Properties.Resources.level_05, squares, statements),
                new Level(Properties.Resources.level_06, squares, statements),
                new Level(Properties.Resources.level_07, squares, statements),
                new Level(Properties.Resources.level_08, squares, statements),
                new Level(Properties.Resources.level_09, squares, statements),
                new Level(Properties.Resources.level_10, squares, statements),
                new Level(Properties.Resources.level_11, squares, statements),
                new Level(Properties.Resources.level_12, squares, statements),
                new Level(Properties.Resources.level_13, squares, statements),
                new Level(Properties.Resources.level_14, squares, statements),
            };

            HashSet<int> ids = new HashSet<int>();
            foreach (Level level in levels)
            {
                if (ids.Contains(level.ID))
                {
                    throw new Exception("A level has an ID already in use. The ID is " + level.ID);
                }
                ids.Add(level.ID);
            }
        }

    }
}
